package com.demo.dialet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.ModelAttribute;

public class PageLimit {
	protected Log logger = LogFactory.getLog(getClass());
	public static final String PAGE_COUNT = "limit.count";
	public static final String PAGE_SIZE = "limit.length";
	public static final String PAGE_START = "limit.start";
	private boolean limited;
	private boolean isLimited;
	private Integer currentPageNo;
	private Integer pageLength;
	private Integer totalCount;
	private Integer startRowNo;
	private Integer endRowNo;
	private boolean onlyGetTotalCnt;
	private String id;
	private String name;
	
	public PageLimit() {
		this.onlyGetTotalCnt = true;
		this.limited = false;
	}

	public PageLimit(boolean limited, Integer pageStart, Integer pageSize, Integer totalCount) {
		this.limited = limited;

		if (limited) {
			this.isLimited = false;

			this.currentPageNo = pageStart;
			this.pageLength = pageSize;
			this.totalCount = Integer.valueOf(totalCount == null ? 0 : totalCount.intValue());

			if (this.currentPageNo.intValue() > 1)
				this.startRowNo = Integer.valueOf((this.currentPageNo.intValue() - 1) * this.pageLength.intValue() + 1);
			else {
				this.startRowNo = this.currentPageNo;
			}

			this.endRowNo = Integer.valueOf(this.startRowNo.intValue() + this.pageLength.intValue() - 1);
		}
	}
	
	public PageLimit(Integer startRowNo, Integer pageLength) {
		this(startRowNo, pageLength,true);
	}
	
	public PageLimit(Integer pageLength, String id, String name){
		this.limited = true;
		this.isLimited = false;
		this.pageLength = (pageLength == null ? Integer.valueOf(20) : pageLength);
		this.totalCount = -1;
		this.startRowNo = 1;
		this.endRowNo = Integer.valueOf(this.startRowNo.intValue() + pageLength.intValue() - 1);
		this.id = id;
		this.name = name;
	}
	
	public PageLimit(Integer pageLength, String id){
		this(pageLength, id, "id");
	}
	
	public PageLimit(Integer startRowNo, Integer pageLength,boolean count) {
		this.limited = true;
		this.isLimited = false;
		this.pageLength = (pageLength == null ? Integer.valueOf(20) : pageLength);
		this.totalCount = count?0:-1;
		this.startRowNo = startRowNo;
		this.endRowNo = Integer.valueOf(this.startRowNo.intValue() + pageLength.intValue() - 1);
	}

	public PageLimit(Integer currentPageNo, Integer pageLength, Integer totalCount) {
		this(currentPageNo,pageLength,totalCount,true);
	}
	
	public PageLimit(Integer currentPageNo, Integer pageLength, Integer totalCount,boolean count) {
		this.limited = true;
		this.isLimited = false;

		this.currentPageNo = Integer.valueOf((currentPageNo == null || currentPageNo == 0) ? 1 : currentPageNo.intValue());
		this.pageLength = (pageLength == null ? Integer.valueOf(20) : pageLength);

		//this.totalCount = Integer.valueOf(totalCount == null ? 0 : totalCount.intValue());
		this.totalCount = count?0:-1;

		if (this.currentPageNo.intValue() > 1)
			this.startRowNo = Integer.valueOf((this.currentPageNo.intValue() - 1) * pageLength.intValue() + 1);
		else {
			this.startRowNo = this.currentPageNo;
		}

		this.endRowNo = Integer.valueOf(this.startRowNo.intValue() + pageLength.intValue() - 1);
	}

	public Integer getStartRowNo() {
		return this.startRowNo;
	}
	
	public void setStartRowNo(Integer startRowNo){
		this.startRowNo = startRowNo;
	}

	public Integer getEndRowNo() {
		return this.endRowNo;
	}

	@ModelAttribute("totalPageCount")
	public Integer getTotalPageCount() {
		Integer totalPageCount = null;

		if (this.limited) {
			if (this.totalCount.intValue() % this.pageLength.intValue() == 0)
				totalPageCount = Integer.valueOf(this.totalCount.intValue() / this.pageLength.intValue());
			else {
				totalPageCount = Integer.valueOf(this.totalCount.intValue() / this.pageLength.intValue() + 1);
			}
		}
		return totalPageCount;
	}

	@ModelAttribute("hasPreviousPage")
	public boolean getHasPreviousPage() {
		return this.currentPageNo.intValue() > 1;
	}

	@ModelAttribute("hasPreviousPage")
	public boolean getHasNextPage() {
		return this.currentPageNo.intValue() < getTotalPageCount().intValue();
	}

	public boolean limited() {
		return this.limited;
	}

	public boolean isLimited() {
		return this.isLimited;
	}

	public void setLimited(boolean isLimited) {
		this.isLimited = isLimited;
	}

	public Integer getTotalCount() {
		return this.totalCount;
	}

	public Integer getCurrentPageNo() {
		return this.currentPageNo;
	}

	public Integer getPageLength() {
		return this.pageLength;
	}

	public void setTotalCount(Integer totalCount) {
		this.isLimited = true;
		this.totalCount = totalCount;
	}

	public boolean isOnlyGetTotalCnt() {
		return this.onlyGetTotalCnt;
	}

	public void setOnlyGetTotalCnt(boolean onlyGetTotalCnt) {
		this.onlyGetTotalCnt = onlyGetTotalCnt;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
}